<?php
namespace app\admin\controller;
use app\common\model\AdminNotify;
use think\Controller;
use think\Request;


// 后台管理员控制
class Admin extends Common
{

    public function index()
    {
        
        return  $this->fetch('admin/admin');
    }

    public function menu(){
        $admin_id=session('admin_id');
        $adminModel = new \app\admin\model\AdminModel();
        $right= $adminModel->getAdminInfoById($admin_id,'admin_right');

        $rightList = \app\admin\model\RightModel::getRightName($right['admin_right']);

        $this->assign('notify_nums',AdminNotify::getAdminNotifyNum($admin_id));
        return $this->fetch('admin/menu',['rightList'=> $rightList]);
    }

    public function postAjaxClearNotifyNum(Request $request)
    {
        $notify_type = $request->post('notify_type');
        if(empty($notify_type)){
            die(json_encode(false));
        }
        $admin_id=session('admin_id');
        if(!$admin_id)
        {
            die(json_encode(false));
        }
        AdminNotify::clearAdminNotifyNum($admin_id,$notify_type);
        die(json_encode(true));
    }
    public function header(){

        $this->assign('name', session('name'));
        return $this->fetch('admin/header');
    }

    public function content(){
        $admin_name=session('name');
        $ip= request()->ip();
        $this->assign([
            'admin_name'=>$admin_name,
            'IP'=>$ip,
            'time'=>time(),
        ]);

        return $this->fetch('admin/content');
    }



    // 管理员列表显示
    public function getAdminList(){

        $this->adminList();
        return $this->fetch('adminRight/adminList');
    }

    protected function adminList()
    {

        $list = Db('admin')->field('admin_id,admin_name,login_time,login_ip,admin_type,admin_right')->paginate(10);
        $page = $list->render();

        $list=$list->items(); //使用paginate方法分页得到的是对象，遍历前需要转换成数组
        foreach ($list as $key => $value) {

            // 将权限值 转换为 model数组
            $list[$key]['admin_right'] = \app\admin\model\RightModel::getRightName($value['admin_right']);


        }
        $admin_amount = Db('admin')->count();



        $right_desc= \app\admin\model\RightModel::$module_rights_desc;
        $right_desc=array_keys($right_desc);

        $this->assign([
            'page' => $page,
            'list' => $list,
            'admin_amount' => $admin_amount,
            'right_desc'=> $right_desc
        ]);
    }

    // 添加管理员
    public function getAdminAdd(){

        $rightList=\app\admin\model\RightModel::$module_rights_desc;
        unset($rightList['admin']); //去掉管理员模块权限，只有超级管理员才可以操作管理员模块
        
        return $this->fetch('adminRight/add',['list'=>$rightList]);
    }

    public function postDoAdminAdd(){
        if(request()->isPost()){

            $admin_name = input('admin_name','');
            $password1 = input('password1','');
            $password2 = input('password2','');
            $admin_type=input('admin_type','');
         
            $admin_right=input('admin_right/a',''); //tp5使用input接受数组需要加 /a


            if(!$admin_name || !$password1 || !$password2 || !$admin_type ){
                $this->error('请填写完整数据再提交','/admin/AdminAdd');
            }
            //
            $adminModel =new \app\admin\model\AdminModel();

            $res=$adminModel->checkoutAdminName($admin_name);
            if($res){
                $this->error('当前管理员名称已存在','/admin/AdminAdd');
            }

            // 
            if($password1 != $password2){
                $this->error('两次输入密码不一致','/admin/AdminAdd');
            }


            if($adminModel->adminAdd(input('post.'))){
                $this->success('添加成功', '/admin/AdminList');die;
            }else{
                $this->error('添加失败','/admin/AdminAdd');
            }
        }
    }

    // 修改管理员 权限
    public function getAdminEdit()
    {

        $id=input('id');

        $adminModel= new \app\admin\model\AdminModel();
        $info= $adminModel->getAdminInfoById($id,'admin_id,admin_name,password,admin_type,admin_right');

        $info['admin_right']= \app\admin\model\RightModel::getRightName($info['admin_right']);

        $rightList = \app\admin\model\RightModel::$module_rights_desc;
        unset($rightList['admin']); 

        return $this->fetch('adminRight/edit', ['info' => $info,'list'=>$rightList]);
    }

    public function postDoAdminEdit(){
        if (request()->isPost()) {

            
            $admin_id = input('admin_id');
            $password1 = input('password1', '');
            $password2 = input('password2', '');
            $admin_type = input('admin_type', '');

            $admin_right = input('admin_right/a', ''); //tp5使用input接受数组需要加 /a


            if (  !$password1 || !$password2 ) {

                $this->error('密码不能为空','/admin/AdminEdit/id/'.$admin_id);
            }

            // 
            if ($password1 != $password2) {
                $this->error('两次输入密码不一致', '/admin/AdminEdit/id/' . $admin_id);
            }


            $adminModel = new \app\admin\model\AdminModel();

            if ($adminModel->adminEdit(input('post.'))) {
                $this->success('修改成功', '/admin/AdminList');die;
            } else {
                $this->error('修改失败', '/admin/AdminEdit/id/' . $admin_id);
            }

        }
    }

    // 删除
    public function getAdminDel(){

        $id = input('id');
        $admin_type=session('admin_type');
        $admin_id=session('admin_id');

        if(!$admin_type == 1){
            $this->error('普通管理员账户无法执行此操作', '/admin/AdminList');
        }

        if($id == $admin_id ){
            $this->error('不能删除当前登录账户','/admin/AdminList');
        }

        
        $res=Db('admin')->where('admin_id',$id)->delete();
        if($res){
            $this->success('删除成功', '/admin/AdminList');die;
        }else{
            $this->error('删除失败', '/admin/AdminList');
        }
    }

}
